<?php

class UsersController extends AppController {

//    public $helpers = array ('Html','Form');

    var $name = 'Users';
    var $helpers = array('html', 'form');
    
    function login() {                  
    //-- code inside this function will execute only when autoRedirect was set to false (i.e. in a beforeFilter).
        if ($this->Auth->login($this->request->data)){
            $this->Session->delete('BIENVENIDO');
            if (!empty($this->request->data['User']['remember_me'])){
                $cookie = array();
                $cookie['username'] = $this->request->data['User']['username'];
                $cookie['password'] = $this->request->data['User']['password'];
                $this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
                unset($this->request->data['User']['remember_me']);
                $this->redirect($this->Auth->redirect());
                $this->Session->setFlash('BIENVENIDO');
            }
            
        }
    }
    
    function logout(){
       $cookie = $this->Cookie->read('Auth.User');
       if($cookie)
            $this->Cookie->delete('Auth.User');
       $this->Session->setFlash('Has cerrado tu Session regresa pronto');
       $this->redirect($this->Auth->logout());
    }
        
    function modificar($id = null) {
        $this->Usuario->id = $id;
        if ($this->request->is('get')) {//Falta la funcion get
            $this->request->data = $this->Usuario->read();
        } else {
            if ($this->Usuario->save($this->request->data)) {
                $this->Session->setFlash('El usuario ha sido modificado.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    function ver() {
        $this->layout = "layout";
        $this->set('usuarios', $this->Usuario->find('all'));
    }

    function eliminar($id) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Usuario->delete($id)) {
            $this->Session->setFlash('El usuario con:' . $id . 'ha sido eliminado.');
            $this->redirect(array('action' => 'index'));
        }
    }

}

?>
